import re
import uuid
from collections.abc import Iterator

from sqlalchemy import ForeignKey
from sqlalchemy.orm import (
    DeclarativeBase,
    declared_attr,
    Mapped,
    mapped_column,
)

PATTERN = r'(?<=\w)(?=[A-Z])'


def _lower_class_name(name: str) -> Iterator[str]:
    for value in re.split(r'(?<=\w)(?=[A-Z])', name):
        yield value.lower()


class Base(DeclarativeBase):
    @declared_attr
    def __tablename__(self) -> str:
        return self.__name__.lower()


class AbstractResume(Base):
    __abstract__ = True
    id: Mapped[uuid.UUID] = mapped_column(
        primary_key=True,
        default=uuid.uuid4
    )
    resume_id: Mapped[uuid.UUID] = mapped_column(ForeignKey('resume.id'))

    @declared_attr
    def __tablename__(self) -> str:
        _class = self.__name__
        return '_'.join(_lower_class_name(_class))
